package com.example.statistics.ui.car

import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.bean.CarInfoBean
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.httpPost
import com.example.statistics.ex.singleDialog
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.example.statistics.utils.activityForResult
import com.example.statistics.weight.TextProcessingView
import com.example.statistics.ex.toUpperCase
import kotlinx.android.synthetic.main.activity_car_edit.*
import java.util.*

class CarEditActivity : BaseActivity() {

    var data: CarInfoBean? = null
    override val title: String
        get() = "车辆信息"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_car_edit)
        data = intent.getSerializableExtra(Constants.DATA) as CarInfoBean?
        val action = intent.getStringExtra(Constants.ACTION).orEmpty()
        refreshInfo(data)
        tv_plate_number.toUpperCase()
        tv_original_plate.toUpperCase()
        tv_plate_number.onSearch("车牌")
        tv_driver_phone.onSearch("手机号")
        tv_logistics_company.onSearch("物流")
        tv_sales_area.onSearch("销售地")
        tv_car_lenght.onContentClicked {
            singleDialog("4.2米", "7.2米", "9.6米", "挂车") {
                tv_car_lenght.contentText = it
            }
        }

        topBarRightText("保存") {
            val params = mutableListOf(
                "plateNumber" to tv_plate_number.contentText?.toUpperCase(Locale.getDefault()),
                "driverPhone" to tv_driver_phone.contentText,
                "logisticsCompany" to tv_logistics_company.contentText,
                "parkingSpace" to tv_parking_space.contentText,
                "salesArea" to tv_sales_area.contentText,
                "carLength" to tv_car_lenght.contentText,
                "originalPlateNumber" to tv_original_plate.contentText
            )

            val httpUrl = if (action == Constants.ACTION_EDIT) {
                params.add("id" to (data?.id)?.toString())
                UrlConfig.CAR_UPDATE
            } else
                UrlConfig.CAR_ADD
            httpPost<CarInfoBean>(httpUrl, config = {
                listParams = params
            }, result = {
                activityFinish()
            })
        }
    }

    private fun TextProcessingView.onSearch(type: String) {
        this.onSymbolClicked { str ->
            activityForResult<CarListActivity>(Constants.SEARCH to str, Constants.TYPE to type) {
                data = it?.getSerializableExtra(Constants.DATA) as CarInfoBean?
                refreshInfo(data)
            }
        }
    }

    private fun refreshInfo(carBean: CarInfoBean?) {
        if (carBean != null) {
            tv_plate_number.contentText = carBean.plateNumber
            tv_driver_phone.contentText = carBean.driverPhone
            tv_logistics_company.contentText = carBean.logisticsCompany
            tv_parking_space.contentText = carBean.parkingSpace
            tv_sales_area.contentText = carBean.salesArea
            tv_car_lenght.contentText = carBean.carLength
            tv_original_plate.contentText = carBean.originalPlateNumber
        }
    }
}